package com.jffccloud.auth.service;

import com.google.common.collect.Lists;
import com.jffccloud.auth.mapper.UserMapper;
import com.jffccloud.auth.model.Privilege;
import com.jffccloud.auth.model.SysUser;
import com.jffccloud.common.service.AbstractServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserService extends AbstractServiceImpl<UserMapper,SysUser> {
    public SysUser findUserByUsername(String username){
        return baseMapper.findUserByUsername(username);
    }

    public Collection<GrantedAuthority> loadUserAuthorities(String userId){
        List<Privilege> ownAuthList = baseMapper.getMenu(userId);
        List<GrantedAuthority> authList = Lists.newArrayList();
        Map map = new HashMap();
        for (Privilege action : ownAuthList) {
            if(StringUtils.isNotBlank(action.getUrl())) {
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(action.getUrl());
                authList.add(grantedAuthority);
            }
            map.put("parentId", action.getId());
            List<Privilege> chilren = baseMapper.getMenuNext(map);
            for (Privilege chil : chilren) {
                if(StringUtils.isNotBlank(chil.getUrl())) {
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(chil.getUrl());
                    authList.add(grantedAuthority);
                }
            }
        }
        return authList;
    }

    public List getMenu(String userId){
        List<Privilege> list = baseMapper.getMenu(userId);
        List<Privilege> tree = new ArrayList();
        tree.addAll(list);
        Map map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            map.put("parentId", list.get(i).getId());
            List<Privilege> chilren = baseMapper.getMenuNext(map);
            if (chilren!=null&&!chilren.isEmpty()) {
                list.get(i).setChildren(chilren);
            }
        }
        return buildMenu(tree);
    }

    public List buildMenu(List<Privilege> list) {
        List li = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Map map = new HashMap();
            Privilege params = list.get(i);
            String url = params.getUrl();
            if (StringUtils.isEmpty(url)) {
                url = null;
            }
            String id = params.getId();
            String name = params.getName();
            String parentId = params.getParentId();
            String iconCls = params.getIcon();
            List children = (List) params.getChildren();
            if (children != null && children.size() > 0) {
                children = this.buildMenu(children);
                map.put("children", children);
            }
            if (StringUtils.isNotEmpty(url)) {
                map.put("url", url);
            }
            map.put("parentId", parentId);
            map.put("name", name);
            map.put("id", id);
            map.put("icon", iconCls);
            li.add(map);
        }
        list = li;
        return list;
    }
}
